package com.eric.code.oauth2.my;

import com.eric.code.oauth2.common.BaseController;
import com.eric.code.oauth2.intercepter.SessionInterceptor;
import com.eric.code.oauth2.login.LoginService;
import com.eric.code.oauth2.model.User;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;

/**
 * Created by eric on 17-3-21.
 */
@Before(SessionInterceptor.class)
public class MyController extends BaseController {

    private MyService srv = MyService.me;

    public void index(){
        setAttr("user", getLoginUser());
        render("edit.html");
    }


    public void update(){
        srv.update(getModel(User.class));
        redirect("/my");
    }

    public void pass(){
        render("password.html");
    }

    @Before(PassValidator.class)
    public void passUpdate(){
        User user = getLoginUser();
        Ret ret = srv.updatePassword(getPara("oldPassword"),getPara("newPassword"),getLoginUser());
        if(ret.isOk()){
            switch (user.getType()){
                case LoginService.USER_NORMAL:
                    redirect("/my");
                    break;
                case LoginService.USER_ADMIN:
                    redirect("/app");
                    break;
            }
        }else{
            setAttr("ret",ret);
            render("/view/error.html");
        }
    }

    public void logout(){
        LoginService.me.logout(getSessionId());
        redirect("/user");
    }


}
